﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;

namespace SalaryChannel.Web
{
    [ServiceContract(Namespace = "http://salary.yangchao.ts.gs.chinamobile/")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class SupportService:ServiceAuthRoot
    {
        [OperationContract]
        public List<Su_List_Type>GetListType()
        {
            // 在此处添加操作实现
            try
            {
                Authoricate();
                using (SupportDataClassesDataContext context = new SupportDataClassesDataContext())
                {
                    var query = from q in context.Su_List_Type
                                orderby q.Type_ID
                                select q;
                    if (query.Any())
                    {
                        return query.ToList();
                    }
                    return new List<Su_List_Type>();
                }
            }
            catch (Exception ex)
            {
                LogManager.Write("SupportService.GetListType:" + ex.Message);
                return null;
            }
        }

        // 在此处添加更多操作并使用 [OperationContract] 标记它们

        [OperationContract]
        public List<string> GetDepartment()
        {
            List<string> delist = new List<string>();
            try
            {
                Authoricate();
                using (SupportDataClassesDataContext context = new SupportDataClassesDataContext())
                {
                   return  context.ExecuteQuery<string>("select '['+Id+']'+ChannelName from ChannelAccount").ToList();
                }
            }
            catch (Exception ex)
            {
                 LogManager.Write("SupportService.GetDepartment:" + ex.Message);
            }
            return delist;
        }

        [OperationContract]
        public List<string> GetUserByDepartment(string DepartID)
        {
            List<string> delist = new List<string>();
            try
            {
                Authoricate();
                using (SupportDataClassesDataContext context = new SupportDataClassesDataContext())
                {
                    var query = from q in context.VW_Su_Department
                                where q.Channel_ID==DepartID
                                orderby q.Priority
                                select new { id = q.User_ID, name = q.UserName };

                    foreach (var d in query)
                    {
                        delist.Add(string.Format("[{0}]{1}", d.id, d.name));
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.Write("SupportService.GetUserByDepartment:" + ex.Message);
                return null;
            }
            return delist;
        }

        [OperationContract]
        public bool CreateWorkList(string CreateID, string Subject, string Body, string ToChannelId)
        {
            try
            {
                Authoricate();

                using (SupportDataClassesDataContext context = new SupportDataClassesDataContext())
                {
                    context.Su_Work_List.InsertOnSubmit(new Su_Work_List
                    {
                        Create_ID = CreateID,
                        Create_Time = DateTime.Now,
                        Subject = Subject,
                        Body = Body,
                        Status_ID = 0,
                        ToChannel_ID = ToChannelId
                    });

                    context.SubmitChanges();

                    return true;
                }

            }
            catch (Exception ex)
            {
                LogManager.Write("SupportService.CreateWorkList:" + ex.Message);
                return false;
            }
        }

        private void Email(string CreateID,string NextUserID,string Subject)
        {
            try
            {
                using (DataClassesQueryDataContext context = new DataClassesQueryDataContext())
                {
                    var query = from q in context.UserInfor
                                where q.User_ID == CreateID || q.User_ID == NextUserID
                                select q;
                   
                    var cUser = from c in query
                                where c.User_ID == CreateID
                                select c.UserName;
                    var nEmail = from n in query
                                 where n.User_ID == NextUserID
                                 select n.Email;
                    if (!cUser.Any() || !nEmail.Any())
                    {
                        LogManager.Write("SupportService.Email: 查询用户信息异常");
                        return;
                    }
                    string Msg = string.Format("您好！您在渠道综合支撑系统有新的待办事宜({0}发):{1}。请尽快处理。",
                        cUser.First(), Subject);
                    this.SendEmail(string.Format("渠道综合支撑系统提示:您有新待办事宜({0}发):{1}",
                        cUser.First(), Subject), Msg, nEmail.First());

                }
            }
            catch (Exception ex)
            {

                LogManager.Write("SupportService.Email:" + ex.Message);
            }

        }


        [OperationContract]
        public List<VW_UserWorkList> GetWorkList(string Subject,  string ChannelId, DateTime Start, DateTime End,
            int Index,int PageSize)
        {
            try
            {
                Authoricate();

                using (SupportDataClassesDataContext context = new SupportDataClassesDataContext())
                {
                    var query = from q in context.VW_UserWorkList
                                where (string.IsNullOrEmpty(Subject)|| q.Subject.Contains(Subject))
                                where (string.IsNullOrEmpty(ChannelId) || q.ToChannel_ID == ChannelId)
                                where q.CreateTime >= Start
                                where q.CreateTime <= End
                                orderby q.CreateTime descending
                                select q;
                        return query.Skip((Index -1)* PageSize).Take(PageSize).ToList();

                    return new List<VW_UserWorkList>();
                }
            }
            catch (Exception ex)
            {
                LogManager.Write("SupportService.GetWorkList:" + ex.Message);
                return null;
            }
        }

        [OperationContract]
        public List<VW_WorkStep> GetWorkStep(int WorkID)
        {
            try
            {
                Authoricate();

                using (SupportDataClassesDataContext context = new SupportDataClassesDataContext())
                {
                    var query = from q in context.VW_WorkStep
                                where q.Work_ID==WorkID
                                orderby q.Start 
                                select q;
                    if (query.Any())
                    {
                        return query.ToList();
                    }
                    return new List<VW_WorkStep>();
                }
            }
            catch (Exception ex)
            {
                LogManager.Write("SupportService.GetWorkStep:" + ex.Message);
                return null;
            }
        }
        [OperationContract]
        public string GetWorkBody(int WorkID)
        {
            try
            {
                Authoricate();

                using (SupportDataClassesDataContext context = new SupportDataClassesDataContext())
                {
                    var query = from q in context.Su_Work_List
                                where q.ID == WorkID
                                select q.Body;
                    if (query.Any())
                    {
                        return query.First();
                    }
                    return string.Empty;
                }
            }
            catch (Exception ex)
            {
                LogManager.Write("SupportService.GetWorkBody:" + ex.Message);
                return null;
            }
        }

        [OperationContract]
        public List<VW_UserWorkList> GetMyWork(string ChannelId)
        {
            try
            {
                Authoricate();

                using (SupportDataClassesDataContext context = new SupportDataClassesDataContext())
                {
                    var query = from q in context.VW_UserWorkList
                                where q.ToChannel_ID == ChannelId
                                where q.Status_ID == 0
                                orderby q.CreateTime 
                                select q;
                    if (query.Any())
                    {
                        return query.Distinct().ToList();
                    }
                    return new List<VW_UserWorkList>();
                }
            }
            catch (Exception ex)
            {
                LogManager.Write("SupportService.GetMyWork:" + ex.Message);
                return null;
            }
        }
        [OperationContract]
        public bool FinishMyWork(int id, string operatorId, bool isDeal, string opinion)
        {
            try
            {
                Authoricate();
                using (SupportDataClassesDataContext context = new SupportDataClassesDataContext())
                {
                    var work = context.Su_Work_List.FirstOrDefault(x => x.ID == id);
                    if (work == null) return false;
                    work.Status_ID = 1;
                    work.Completed_Time = DateTime.Now;
                    work.Completed_User = operatorId;
                    work.IsDeal = isDeal;
                    work.Opinion = opinion;
                    context.SubmitChanges();                   
                    return true;
                }
            }
            catch (Exception ex)
            {
                 LogManager.Write("SupportService.FinishMyWork: " + ex.ToString());
                        return false;
            }
        }
    }

    public enum WorkStep
    {
        Translation,
        Completed,
        Archivement
    }
}
